/*
 *      class Request : http请求类
*/


var default_host = "https://www.bigllke.xyz/time/"
//var default_host = "https://47.94.240.95:8080/"
var default_timeout = 5000

export class Request {
    host = default_host
    timeout = default_timeout
    Request() {
        this.host = default_host
        this.timeout = default_timeout
    }
    setTimeout(time) {
        this.timeout = time
    }
    get(path, getData, successFun, failFun = null) {
        console.log('request.get   ' + this.host + path)
        wx.request({
            url: this.host + path,
            header: {
                'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            data: getData,
            method: "GET",
            dateType: 'json',
            responseType: 'text',
            timeout: this.timeout,
            success: function (res) {
                successFun(res)
            },
            fail: function () {
                if (failFun == null) {
                    wx.showToast({
                        title: "服务器通信失败，请重试",
                        icon: "none"
                    })
                } else {
                    failFun()
                }
            }
        });
    }
    post(path, postData, successFun, failFun = null) {
        console.log('request.post   ' + this.host + path)
        wx.request({
            url: this.host + path,
            header: {
                'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
            },
            data: postData,
            method: "POST",
            dateType: 'json',
            responseType: 'text',
            timeout: this.timeout,
            success: function (res) {
                successFun(res);
            },
            fail: function () {
                if (failFun != null) {
                    failFun()
                } else {
                    wx.showToast({
                        title: "服务器通信失败,请重试",
                        icon: "none"
                    });
                }
            }
        })
    }

    uploadFile(url, imgPaths, count, fielName, data, doSuccess, doFail, complete) {
        wx.uploadFile({
            url: host + url, //此链接为你小程序后端的接收文件的请求地址
            filePath: imgPaths[count],
            name: fielName,
            header: {
                "Content-Type": "multipart/form-data"
            },
            formData: {//formDta 有什么用
                data: data
            },
            success: function (res) {
                doSuccess(res);
            }, fail: function (e) {
                doFail();
            }, complete: function (e) {
                complete();
            }
        })
    }
}

export function newRequest() {
    return new Request()
}